package com.sora.sora.server.controller;

import com.sora.sora.common.model.param.ImgParam;
import com.sora.sora.common.model.vo.ImgVO;
import com.sora.sora.common.model.vo.ResultVO;
import com.sora.sora.common.utils.ResponseResult;
import com.sora.sora.server.service.ImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Api(value = "浏览图片接口", tags = "图片区接口")
@RestController
@RequestMapping("/img")
public class ImgController {

    @Resource
    ImgService imgService;

    @Resource
    ResponseResult responseResult;


    @ApiOperation(value = "获得所有图片条数")
    @GetMapping("/count")
    public ResultVO count() { return responseResult.ok(imgService.count());}


//    @ApiOperation(value = "获得总数据条数")
//    @ApiImplicitParam(name = "tags", value = "选中的tag", required = false, dataType = "int", paramType = "query", allowMultiple = true)
//    @GetMapping("/count")
//    public ResultVO count(int[] tags) {
//        return responseResult.ok(imgService.count(tags));
//    }


    @ApiOperation(value = "获得所有图片，按时间降序排列")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),

    })
    @GetMapping
    public ResultVO getAll(int page, int limit) {
        return responseResult.ok(imgService.getAll(page, limit));
    }


//    @ApiOperation(value = "在条件限制下查询记录数量，提供的查询条件有：time:asc/desc")
//    @GetMapping("/count")
//    public ResultVO getCountLimit(Map<String, Object> map) {
//        if (map.isEmpty()) {
//            return responseResult.valid("传入参数为空");
//        } else if (map.containsKey("time")) {
//
//        }
//    }


    @ApiOperation(value = "根据条件查找图片，可供查询的条件有: tags:int[], name:str, uid:int, uploader:str")
    @GetMapping("/query")
    public ResultVO query(Map<String, Object> map, int page, int limit) {
        if (map.isEmpty()) {
            return responseResult.valid("传入参数为空");
        }

        List<ImgVO> list = new LinkedList<>();
        for (String param : map.keySet()) {

        }

        return responseResult.ok(list);
    }

    @ApiOperation(value = "批量删除图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "imgIds", value = "要删除的图片id", required = true, dataType = "int", paramType = "query", allowMultiple = true)
    })
    @DeleteMapping
    public ResultVO delteImg(int uid, int[] imgIds) {
        if (imgIds.length == 0) {
            return responseResult.valid("传入数组为空");
        }
        List<Integer> list = imgService.deleteImgs(uid, imgIds);
        return list.isEmpty() ? responseResult.ok() : responseResult.faile(list);
    }


    @ApiOperation(value = "批量上传图片")
    @PostMapping(headers = "content-type=multipart/form-data")
    public ResultVO uploadImg(@RequestBody @Validated ImgParam[] param) {return null;}


}
